<?php
namespace app\admin\controller;

use app\BaseController;
use app\common\component\Upload;

class Ueditor extends BaseController {

    public function upload(){
        $config = config('app')['ueditorConfig'];
        $action = input('get.action');
        $route = explode('.' , empty($_SERVER['HTTP_HOST']) ? '' : $_SERVER['HTTP_HOST'] );
        $platform = 'ueditor';
        switch($action){
            case 'config':
                $result =  json_encode($config);
                break;
            case 'uploadBase64':
                $code = input('post.code');
                $type = input('post.type' , 'img');
                $result = Upload::instance()->uploadBase64($code , $platform , $type);
                $result = json_encode($result);
                break;
            /* 上传图片 */
            case 'uploadimage':
            /* 上传涂鸦 */
            case 'uploadscrawl':
            /* 上传视频 */
            case 'uploadvideo':
            /* 上传文件 */
            case 'uploadfile':
                $result = Upload::instance()->uploadFile($action , $platform);
                $result = json_encode($result['data']);
                break;
            /* 列出图片 */
            case 'listimage':
                $result = include("action_list.php");
                break;
            /* 列出文件 */
            case 'listfile':
                $result = include("action_list.php");
                break;
            /* 抓取远程文件 */
            case 'catchimage':
                $result = include("action_crawler.php");
                break;
            default:
                $result = json_encode(array(
                    'state'=> '请求地址出错'
                ));
                break;
        }
        /* 输出结果 */
        if (isset($_GET["callback"])) {
            if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
                echo htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
            } else {
                echo json_encode(array(
                    'state'=> 'callback参数不合法'
                ));
            }
        } else {
            echo $result;exit;
        }
    }
}